﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Chisel.Core.Connections
{
	/// <summary>
	/// Anchors a connection to a specific point.
	/// </summary>
	public class Anchor : UserControl, ISegmentEnd
	{
		internal Anchor(Connection con)
			: base()
		{
			_connection = con;
			_circle = new Ellipse() {
				Width = _size,
				Height = _size,
				Fill = new SolidColorBrush(Colors.Black)
			};
			Content = _circle;
		}

		private Connection _connection;

		private Ellipse _circle;

		/// <summary>
		/// The global position of the anchor.
		/// </summary>
		public Point Position { get; set; }

		/// <summary>
		/// The position of the anchor in connection local coordinates.
		/// </summary>
		public Point LocalPosition { get; private set; }

		/// <summary>
		/// Currently, anchors don't have directions.
		/// </summary>
		public Point? Direction
		{
			get { return null; }
			set { }
		}

		private double _size = 8;
		/// <summary>
		/// The size (diameter) of the anchor.
		/// </summary>
		public double Size
		{
			get { return _size; }
			set
			{
				_size = value;
				_circle.Width = _size;
				_circle.Height = _size;
			}
		}

		/// <summary>
		/// Layout the anchor in the connection canvas.
		/// </summary>
		public void Relayout()
		{
			LocalPosition = _connection.GlobalToLocal(Position);
			Canvas.SetLeft(this, LocalPosition.X - Size / 2);
			Canvas.SetTop(this, LocalPosition.Y - Size / 2);
		}
	}
}
